package appli_Oxygene.vues;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

import javax.swing.JComboBox;

import framework.Utilisateurs;
import utilisateur.Admin;
import utilisateur.Utilisateur;

public class AjoutU extends JFrame {

	private JPanel contentPane;
	private JTextField nom_tfd;
	private JTextField prenom_tfd;
	JComboBox<String> type_cbb;
	private JLabel lblLogin;
	private JTextField login_tfd;
	private JLabel lblMotDePasse;
	private JTextField mdp_tfd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AjoutU frame = new AjoutU();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AjoutU() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNom = new JLabel("Nom");
		lblNom.setBounds(62, 24, 107, 14);
		contentPane.add(lblNom);

		nom_tfd = new JTextField();
		nom_tfd.setBounds(176, 21, 199, 20);
		contentPane.add(nom_tfd);
		nom_tfd.setColumns(10);

		JLabel lblPrnom = new JLabel("Pr\u00E9nom");
		lblPrnom.setBounds(62, 54, 107, 16);
		contentPane.add(lblPrnom);

		prenom_tfd = new JTextField();
		prenom_tfd.setBounds(176, 52, 199, 20);
		contentPane.add(prenom_tfd);
		prenom_tfd.setColumns(10);

		JButton btnValider = new JButton("Valider");
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ajouteU();
			}
		});
		btnValider.setBounds(174, 223, 91, 23);
		contentPane.add(btnValider);

		JLabel lblProfil = new JLabel("Profil");
		lblProfil.setBounds(62, 92, 46, 14);
		contentPane.add(lblProfil);

		type_cbb = new JComboBox<>();
		type_cbb.setBounds(176, 90, 199, 22);
		type_cbb.addItem("Utilisateur");
		type_cbb.addItem("Administrateur");
		contentPane.add(type_cbb);

		lblLogin = new JLabel("Login");
		lblLogin.setBounds(62, 141, 107, 14);
		contentPane.add(lblLogin);

		login_tfd = new JTextField();
		login_tfd.setColumns(10);
		login_tfd.setBounds(176, 138, 199, 20);
		contentPane.add(login_tfd);

		lblMotDePasse = new JLabel("Mot de passe");
		lblMotDePasse.setBounds(62, 173, 107, 16);
		contentPane.add(lblMotDePasse);

		mdp_tfd = new JTextField();
		mdp_tfd.setColumns(10);
		mdp_tfd.setBounds(176, 173, 199, 20);
		contentPane.add(mdp_tfd);
	}

	public void ajouteU() {
		if (!nom_tfd.getText().toString().equals("") && !prenom_tfd.getText().toString().equals("")
			&& !login_tfd.getText().toString().equals("") && !mdp_tfd.getText().toString().equals("")) {
			if (type_cbb.getSelectedItem().toString().equals("Administrateur")) {
				try {
					Utilisateurs.ajouteUtilisateur(new Admin(nom_tfd.getText(),
							prenom_tfd.getText(), login_tfd.getText(), mdp_tfd
									.getText()));
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				if (type_cbb.getSelectedItem().toString().equals("Utilisateur")) {
					try {
						Utilisateurs.ajouteUtilisateur(new Utilisateur(nom_tfd
								.getText(), prenom_tfd.getText(), login_tfd
								.getText(), mdp_tfd.getText()));
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			Utilisateurs.sauverUtilisateurs();
			this.dispose();
		} else {
			JOptionPane.showMessageDialog(null,
					"Veuillez remplir tous les champs", "Erreur",
					JOptionPane.ERROR_MESSAGE);
			
		}
	}
}
